using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Core.Toolkit.Core.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class ZY_SafetyCardService : BaseAppService<ZY_SafetyCardDto, ZY_SafetyCard, ZY_SafetyCardRepository>
    {
        readonly ExcelHelp _excelHelp;

        readonly UserIdentity _userIdentity;
        public ZY_SafetyCardService(ZY_SafetyCardRepository repository, ExcelHelp excelHelp, IHttpContextAccessor accessor) : base(repository)
        {
            _excelHelp = excelHelp;
            _userIdentity = accessor.HttpContext.User.GetUserIdentity();
        }



        private LoadPageOffsetModel<ZY_SafetyCard, ZY_SafetyCardDto>
            GetLoadPageOffsetMode(PagingModel<ZY_SafetyCardDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModel(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();

            var dtoModel = pagingQueryModel.Model;
            var filterList = this.CreateQRList()
           .Add("ConfirmationCardNumber", "in"); //编号
            if (_userIdentity.Headoffice)
                filterList.Add("CreateFactory", "="); //总部从查询条件取工厂DI
            else
                filterList.Add("CreateFactory", "=", _userIdentity.Factory);
            var filterExpre = filterList.BuildeQR(dtoModel.ToEntity<ZY_SafetyCard>());
            loadPageOffsetModel.QueryWhere = filterExpre;

            //.Add("OperatingUnit", "=") //作业单位
            //.Add("ProjectUnitLeader", "=") //车间项目负责人
            //.Add("JobContent", "=") //作业内容
            //.Add("EstimateBeginTime", "=") //预计开始时间
            //.Add("JobStartTime", "=") //作业开始时间
            //.Add("JobEndTime", "=") //作业结束时间
            //.Add("JobPerson", "=") //作业人员
            //.Add("BaseRequire", "=") //基本要求
            //.Add("ScopeAndEnvironment", "=") //作业许可范围及作业环境
            //.Add("OperationalRisk", "=") //作业风险
            //.Add("PreventiveMeasures_Title", "=") //防范措施
            //.Add("EmergencyMeasure", "=") //应急措施
            //.Add("OtherConsiderations", "=") //其他注意事项
            //.Add("CreateFactory", "=") //隶属工厂
            //.Add("CreateDept", "=") //
            //.Add("UpdateDept", "=") //
            //.Add("BusinessId", "=");//业务表Id

            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new ZY_SafetyCard()
            {
                Id = model.Id,
                ConfirmationCardNumber = model.ConfirmationCardNumber,//编号
                OperatingUnit = model.OperatingUnit,//作业单位
                ProjectUnitLeader = model.ProjectUnitLeader,//车间项目负责人
                JobContent = model.JobContent,//作业内容
                EstimateBeginTime = model.EstimateBeginTime,//预计开始时间
                JobStartTime = model.JobStartTime,//作业开始时间
                JobEndTime = model.JobEndTime,//作业结束时间
                JobPerson = model.JobPerson,//作业人员
                BaseRequire = model.BaseRequire,//基本要求
                ScopeAndEnvironment = model.ScopeAndEnvironment,//作业许可范围及作业环境
                OperationalRisk = model.OperationalRisk,//作业风险
                PreventiveMeasures_Title = model.PreventiveMeasures_Title,//防范措施
                EmergencyMeasure = model.EmergencyMeasure,//应急措施
                OtherConsiderations = model.OtherConsiderations,//其他注意事项
                CreateFactory = model.CreateFactory,//隶属工厂
                CreateDept = model.CreateDept,//
                UpdateDept = model.UpdateDept,//
                BusinessId = model.BusinessId,//业务表Id
            };
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<ZY_SafetyCardDto>> GetList(PagingModel<ZY_SafetyCardDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string>
            Export(PagingModel<ZY_SafetyCardDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.ConfirmationCardNumber,
                ConfirmationCardNumber_title = "编号",
                model.OperatingUnit,
                OperatingUnit_title = "作业单位",
                model.ProjectUnitLeader,
                ProjectUnitLeader_title = "车间项目负责人",
                model.JobContent,
                JobContent_title = "作业内容",
                model.EstimateBeginTime,
                EstimateBeginTime_title = "预计开始时间",
                model.JobStartTime,
                JobStartTime_title = "作业开始时间",
                model.JobEndTime,
                JobEndTime_title = "作业结束时间",
                model.JobPerson,
                JobPerson_title = "作业人员",
                model.BaseRequire,
                BaseRequire_title = "基本要求",
                model.ScopeAndEnvironment,
                ScopeAndEnvironment_title = "作业许可范围及作业环境",
                model.OperationalRisk,
                OperationalRisk_title = "作业风险",
                model.PreventiveMeasures_Title,
                PreventiveMeasures_Title_title = "防范措施",
                model.EmergencyMeasure,
                EmergencyMeasure_title = "应急措施",
                model.OtherConsiderations,
                OtherConsiderations_title = "其他注意事项",
                model.CreateFactory,
                CreateFactory_title = "隶属工厂",
                model.CreateDept,
                CreateDept_title = "",
                model.UpdateDept,
                UpdateDept_title = "",
                model.BusinessId,
                BusinessId_title = "业务表Id",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

    }

}
